/*
 * Copyright 2009 (c) Patrick Pietens.
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */
package indigo.vo
{
	import indigo.utils.IDisposable;
	import indigo.utils.IParser;

	import flash.events.Event;
	import flash.utils.describeType;
	import flash.utils.getQualifiedClassName;

	/**
	 * <code>ValueObjects</code> are <code>Objects</code> that have no 
	 * functionality but typed data.
	 * 
	 * <p>For example, you can store every single property of a user - his name,
	 *  emailaddress etc.-  in your <code>ModelLocator</code>. 
	 * However, it's better practice to store all properties in a single 
	 * <code>ValueObject</code> and save it in your <code>ModelLocator</code>.
	 * </p>
	 * 
	 * @example First example parses data when created. This method is recommend 
	 * for games and other applications where processing time is an issue.
	 * <listing version="3.0">
	 * </listing>
	 * 
	 * @example Second example parses data at runtime. This method is 
	 * recommended for websites and most applications.
	 * <listing version="3.0">
	 * </listing>
	 * 
	 * @playerversion Flash 9
	 * @langversion ActionScript 3.0
	 */
	public class ValueObject implements IDisposable, IParser
	{
		/**
		 * Object that indicates the raw data of the ValueObject
		 */
		private var _data:*;


		/**
		 * Constructor
		 * @param Object that represents the raw data
		 * 
		 */
		public function ValueObject( data:* = null )
		{
			if ( data != null )
			{
				this.parseData( data );
			}
		}


		/**
		 * Parses the raw data of the ValueObject
		 * @param Object that represents the raw data
		 *  
		 */
		public function parseData( data:* ):void
		{
			this._data = data;
		}


		/**
		 * Object that indicates the raw data of the ValueObject
		 * 
		 */
		public function get data():*
		{
			return this._data;
		}


		/**
		 * Returns a String that represents the ValueObject
		 * @return	String representation of the Value Object
		 * 
		 */
		public function toString():String
		{
			// Get the className of the ValueObject
			var myClassName:String = getQualifiedClassName( this ).split( "::" )[ 1 ];

			// Get the XML description
			var myXML:XMLList = describeType( this ).variable as XMLList;

			// Set local variables
			var myRepresentation:String = "[" + myClassName;
			var myProperty:String;

			// Loopt through all properties
			var i:int = myXML.length();
			while ( i-- > 0 )
			{
				myProperty = myXML[ i ].@name;
				myRepresentation += " " + myProperty + "=" + this[ myProperty ] + " ";
			}

			// Return String representation
			return myRepresentation + "]";
		}


		/**
		 * @inheritDoc
		 * 
		 */
		public function dispose( event:Event = null ):void
		{
			this._data = null;
		}
	}
}